package com.sdcqjy.property.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sdcqjy.property.R;
import com.sdcqjy.property.activity.DetailsActivity;
import com.sdcqjy.property.adapter.FragmentListAdapter;
import com.sdcqjy.property.base.BaseLazyFragment;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.MainListPresenter;
import com.sdcqjy.property.presenter.contract.MainListContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.sdcqjy.property.MyConstants.ChangeDataType;

/**
 * Created by 磁磁帅 on 2018/3/29.
 * 功能：
 */

public class ZiChanFragment extends BaseLazyFragment<MainListPresenter> implements MainListContract.View
        , FragmentListAdapter.OnAdapterClickListener {
    @BindView(R.id.mContentRecyclerView)
    RecyclerView mContentRecyclerView;
    Unbinder unbinder;
    private int type = 2;
    private List<ListMsgMode> modeList;
    private FragmentListAdapter adapter;
    private boolean cityRef = false;

    public ZiChanFragment() {
        presenter = new MainListPresenter(this);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chan_quan, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public void sendMessage(int type, Object object) {
        switch (type) {
            case ChangeDataType:
                cityRef = true;
                break;
            case 1:
                if (modeList != null) {
                    initVIewData();
                }
                break;
        }
    }

    public void initVIew(View view) {
        modeList = new ArrayList<>();
        adapter = new FragmentListAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        mContentRecyclerView.setAdapter(adapter);
        mContentRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    public void initVIewData() {
        presenter.getList(type);
    }

    public void onResume() {
        super.onResume();
        if (isNowVisible) viewVisible();
    }

    public void viewVisible() {
        if (cityRef) {
            cityRef = false;
            presenter.getList(type);
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void onAdapterClicked(ListMsgMode mode) {
        DetailsActivity.open(getActivity(), mode);
    }

    public void getListRet(List<ListMsgMode> list) {
        modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void getListTitle(int[] titleInt) {
        adapter.setTitleInt(titleInt);
    }
}
